#include "tracksmanagerlabel.h"
#include "gps_track.h"

TracksManagerLabel::TracksManagerLabel(GPS_track* t) :
    FTLabel("xxx")
{
    track = t;
    updateCaption();
    setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding );
}

void TracksManagerLabel::updateCaption()
{
    setText(QString("%1\n%2").arg( track->title()).arg( track->description()));
}

void TracksManagerLabel::paintEvent(QPaintEvent * pe)
{
    QPainter painter(this);
    QList<int> legs = track->listLegs();
    QColor leg_color;
    QRect bg_rect = rect();

    int n_legs = legs.count();

    if ( n_legs > 0 )
    {
        int step = width() / n_legs;

        bg_rect.setRight( step );
        foreach (int i, track->listLegs() )
        {
            const GPS_leg* leg = track->leg( i );
            if ( leg != NULL )
            {
                leg_color = leg->color();
                painter.fillRect( bg_rect, QBrush( leg_color ) );
                bg_rect.translate( step, 0 );
            }
        }
    }
    FTLabel::paintEvent(pe);
}

void TracksManagerLabel::mouseReleaseEvent(QMouseEvent *)
{
    emit editTrack(track->id());
}
